// pages/think/think.js

var util = require('../../utils/util.js');

const app = getApp();

Page({

  /**
   * 页面的初始数据 所在位置
   */
  data: {
    file_width: 0,
    file_height: 0,
    file_type: '',
    eye_auth: 0,
    location_auth: false,
    location_info: '所在位置',
    location_info_font_size: 27,
    thinkInfo:'',
    locationInfo: { name: '', address: '', latitude: '', longitude: '' },
    imageList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * check
   */
  checkIn: function () {
    if (this.data.thinkInfo.length > 0){
      if(this.data.imageList.length > 0){
        wx.uploadFile({
          url: 'http://localhost:7001/thinkWhitImage',
          filePath: this.data.imageList[0],
          name: 'file',
          formData: {
            'thinkInfo': this.data.thinkInfo,
            'latitude': this.data.locationInfo.latitude,
            'longitude': this.data.locationInfo.longitude,
            'name': this.data.locationInfo.name,
            'address': this.data.locationInfo.address,
            'eyeInfo': this.data.eye_auth,
            'type': this.data.file_type,
            'width': this.data.file_width,
            'height': this.data.file_height,
            'thirdSessionId': wx.getStorageSync('thirdSessionId')
          },
          success: function (res) {
            var data = JSON.parse(res.data)
            if (data.code == 0) {
              console.log(data.data.uuid)
              wx.redirectTo({
                url: '../canvas/canvas?thinkId=' + data.data.uuid
              })
            }else{
              wx.showToast({
                title: 'Failing',
                icon: 'loading'
              })
            }
          },
          fail: function () {
            wx.showToast({
              title: 'Failing',
              icon: 'loading'
            })
          }
        })
      }else{
        wx.request({
          url: 'http://localhost:7001/thinkNoneImage',
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            'thinkInfo': this.data.thinkInfo,
            'latitude': this.data.locationInfo.latitude,
            'longitude': this.data.locationInfo.longitude,
            'name': this.data.locationInfo.name,
            'address': this.data.locationInfo.address,
            'eyeInfo': this.data.eye_auth,
            'thirdSessionId': wx.getStorageSync('thirdSessionId')
          },
          success: function (res) {
            if (res.data.code == 0) {
              console.log(res.data.data.uuid)
              wx.redirectTo({
                url: '../canvas/canvas?thinkId=' + res.data.data.uuid
              })
            } else {
              wx.showToast({
                title: 'Failing',
                icon: 'loading'
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: 'Failing',
              icon: 'loading'
            })
          }
        })
      }
    }else{
      console.log('无法完成签到...')
    }
  },

  /**
   * 分享开关按钮
   */
  switchChange: function(e){
    if (e.detail.value){
      this.setData({
        eye_auth: 0
      })
    }else{
      this.setData({
        eye_auth: 1
      })
    }
  },
  /**
   * 输入框(绑定输入框是去焦点，重新赋值)
   */
  thinkInput: function(e){
    this.setData({
      thinkInfo: e.detail.value
    })
  },

  /**
   * 获取地址
   */
  locationInfo: function(e){
    this.setData({
      location_info_font_size: 27,
      location_auth: false,
      location_info: '所在位置',
      locationInfo: { name: '', address: '', latitude: '', longitude: '' },
    });
    if (wx.getStorageSync('locationAuth')){
      wx.chooseLocation({
        success: res => {
          if (res.name != null && res.name.length > 24) {
            this.setData({
              location_info_font_size: 18,
              location_auth: true,
              location_info: res.name,
              locationInfo: res,
            });
          } else if (res.name != null && res.name.length > 0){
            this.setData({
              location_info_font_size: 27,
              location_auth: true,
              location_info: res.name,
              locationInfo: res,
            });
          }
        }
      })
    }else{
      wx.openSetting({
        success: res => {
          wx.authorize({
            scope: 'scope.userLocation',
            success: res => {
              wx.setStorageSync('locationAuth', true)
              wx.chooseLocation({
                success: res => {
                  if(res.name != null && res.name.length > 24){
                    this.setData({
                      location_info_font_size: 18,
                      location_auth: true,
                      location_info: res.name,
                      locationInfo: res,
                    });
                  } else if (res.name != null && res.name.length > 0){
                    this.setData({
                      location_info_font_size: 27,
                      location_auth: true,
                      location_info: res.name,
                      locationInfo: res,
                    });
                  }
                }
              })
            }
          })
        }
      })
    }
  },

  /**
   * 选择图片，默认选择一张
   */
  chooseImage: function () {
    wx.chooseImage({
      count: 1 - this.data.imageList.length,
      success: (res) => {
        this.setData({
          imageList: this.data.imageList.concat(res.tempFilePaths)
        })
        wx.getImageInfo({
          src: this.data.imageList[0],
          success: res => {
            this.setData({
              file_type: res.type,
              file_width: res.width,
              file_height: res.height
            })
          }
        })
      }
    })
  },
  /**
   * 预览图片
   */
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  /**
   * 删除图片
   */
  delImage: function (e) {
    util.removeByValue(this.data.imageList, e.target.dataset.src)
    this.setData({
      imageList: this.data.imageList
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})